package com.ct.springmvc.demo;


import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    List<MessageInfo> messageInfos = new ArrayList<>();

    @RequestMapping("/getList")
    public List<MessageInfo> getList () {
        return messageInfos;
    }

    @RequestMapping(value = "/publish", produces = "application/json")
    public String publish (@RequestBody MessageInfo messageInfo) {
        System.out.println(messageInfo);
        if (!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getMsg())
                || !StringUtils.hasLength(messageInfo.getTo())) {
            return "{\"ok\" : 0}";
        }
        messageInfos.add(messageInfo);
        return "{\"ok\" : 1}";
    }
}
